package com.xia.service.impl;

import com.xia.dao.users.IChatDao;
import com.xia.dao.users.IUsersDao;
import com.xia.domian.Chat;
import com.xia.service.IChatService;
import com.xia.util.common.Result;
import com.xia.util.websocket.Websocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class IChatServiceImpl implements IChatService {
    private IChatDao chatDao;
    private IUsersDao usersDao;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    public void setUsersDao(IUsersDao usersDao) {
        this.usersDao = usersDao;
    }
    @Autowired
    public void setChatDao(IChatDao chatDao) {
        this.chatDao = chatDao;
    }

    @Override
    public List<Chat> selectUsersChatMsg(Integer usersId) {
        List<Chat> chatList=chatDao.selectUsersChat(usersId);
        List<Chat> chats=new ArrayList<>();
        for (Chat chat : chatList) {
            //查询的是被交流对象的信息
            if (chat.getChatUser().equals(usersId)){
                chat.setUsers(usersDao.selectUsersById(chat.getChatFriend()));
            }else{
                chat.setUsers(usersDao.selectUsersById(chat.getChatUser()));
            }
            ListOperations<String, Object> listOps = redisTemplate.opsForList();
            Long listSize=listOps.size(chat.getChatKey());
            List<Object> lists;
            if (listSize==1){
                lists = listOps.range(chat.getChatKey(), 0, 0);
            }else {
                lists = listOps.range(chat.getChatKey(), listSize-1, listSize);
            }
            chat.setNewMessage((String) lists.get(0));
            chats.add(chat);
        }
        return chats;
    }

    @Override
    public void userOnTheChat(Chat chat) throws Exception{
        Chat chatIsExist=chatDao.selectChatIsExist(chat.getChatUser(),chat.getChatFriend());
            ListOperations<String, Object> listOps = redisTemplate.opsForList();
            Long insertState=listOps.rightPush(chatIsExist.getChatKey(),chat.getNewMessage());
            if (insertState!=0){
                Websocket.sendMessage(String.valueOf(chat.getChatFriend()),chat);
            }
    }

    @Override
    public Map<String, Object> getChatContentAndByUser(String chatKey, Integer byUsersId) {
        Result result = Result.success();
        ListOperations<String,Object> listOperations=redisTemplate.opsForList();
        List<Object> list=listOperations.range(chatKey,0,-1);
        result.put("chatContent",list);
        result.put("byUser",usersDao.selectUsersById(byUsersId));
        return result;
    }

    @Override
    public Map<String,Object> privateLetter(Integer usersId, Integer forUsersId) {
        Map<String,Object> map=new HashMap<>();
        Chat chat=chatDao.selectChatIsExist(usersId,forUsersId);
        if (chat!=null){
            map.put("forUsersId",forUsersId);
            map.put("chatKey",chat.getChatKey());
        }else {
            Chat chatInsert=new Chat();
            chatInsert.setChatUser(usersId);
            chatInsert.setChatFriend(forUsersId);
            UUID key=UUID.randomUUID();
            chatInsert.setChatKey(key.toString());
            Integer insertState=chatDao.insertChat(chatInsert);
            if (insertState == 0){
                return null;
            }
            ListOperations<String, Object> listOps = redisTemplate.opsForList();
            String newMessage="你好哇/"+usersId;
            listOps.rightPush(chatInsert.getChatKey(),newMessage);
            map.put("forUsersId",forUsersId);
            map.put("chatKey",chatInsert.getChatKey());
        }
        return map;
    }


}
